package com.it;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvcConfigurerAdapter是什么？
 * Spring内部的一种配置方式
 * 采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
 *
 * WebMvcConfigurerAdapter常用的方法
 *  解决跨域问题 void addCorsMappings(CorsRegistry registry);
 *  添加拦截器 void addInterceptors(InterceptorRegistry registry);
 *  配置视图解析器 void configureViewResolvers(ViewResolverRegistry registry);
 *  配置内容裁决的一些选项 void configureContentNegotiation(ContentNegotiationConfigurer configurer);
 *  跳转指定页面-视图跳转控制器 void addViewControllers(ViewControllerRegistry registry);
 *  静态资源处理 void addResourceHandlers(ResourceHandlerRegistry registry);
 *
 * WebMvcConfigurerAdapter使用方式
 * 1、过时方式：继承WebMvcConfigurerAdapter
 * 2、现用方式
 * 1）实现WebMvcConfigurer
 * 2）现用方式：继承WebMvcConfigurationSupport
 */
@SpringBootApplication
public class YouApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(YouApplication.class, args);
        //打印容器中加载的Bean
//        Arrays.asList(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);

//        SpringApplication springApplication = new SpringApplication(YouApplication.class);
//        springApplication.run();
    }

    private static String[] excludes = new String[]{"/pay/**"};

    @Bean
    public FilterRegistrationBean httpFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new HttpFilter());
        registrationBean.addUrlPatterns("/threadLocal/*");
        return registrationBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/cors/**")
                .allowedHeaders("*")
                .allowedMethods("POST","GET")
                .allowedOrigins("*");
    }

}
